package com.ly.web.controller.erp;

import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.heart.domain.KingFinanceReceiptSettlementBillEntity;
import com.ly.heart.domain.dto.KingFinanceReceiptBillDto;
import com.ly.heart.domain.dto.KingFinanceReceiptBillReciteDto;
import com.ly.heart.domain.dto.KingFinanceReceiptBillStatusDto;
import com.ly.heart.service.IKingFinanceReceiptBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.KingFinanceReceiptBillEntity;

import javax.annotation.Resource;

/**
 * 财务模块-应收票据,推金蝶Controller
 *
 * @author wutao
 * @date 2024-06-19
 */
@Api(value = "KingFinanceReceiptBill", tags = "财务模块-应收票据,推金蝶")
@RestController
@RequestMapping("/heart/KingFinanceReceiptBill")
public class KingFinanceReceiptBillController extends BaseController {

    @Resource
    private IKingFinanceReceiptBillService kingFinanceReceiptBillEntityService;

    /**
     * 查询财务模块-应收票据,推金蝶列表
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:list')")
    @GetMapping("/list")
    @ApiOperation("查询财务模块-应收票据,推金蝶列表")
    public AjaxResult list(KingFinanceReceiptBillEntity kingFinanceReceiptBillEntity) {
        AjaxResult ajaxResult = kingFinanceReceiptBillEntityService.selectKingFinanceReceiptBillEntityList(kingFinanceReceiptBillEntity);
        return ajaxResult;
    }

    /**
     * 获取财务模块-应收票据,推金蝶详细信息
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取财务模块-应收票据,推金蝶详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(kingFinanceReceiptBillEntityService.selectKingFinanceReceiptBillEntityById(id));
    }

    /**
     * 新增财务模块-应收票据,推金蝶
     */
    //@PreAuthorize ( "@ss.hasPermi('heart:KingFinanceReceiptBill:add')" )
    @Log(title = "财务模块-应收票据,推金蝶", businessType = BusinessType.INSERT)
    @PostMapping("draft")
    @ApiOperation("新增财务模块-应收票据,推金蝶")
    public AjaxResult draft(@RequestBody KingFinanceReceiptBillEntity kingFinanceReceiptBillEntity) {
        if (StringUtils.isNotBlank(kingFinanceReceiptBillEntity.getId())) {
            return kingFinanceReceiptBillEntityService.updateKingFinanceReceiptBillEntity(kingFinanceReceiptBillEntity);
        }
        return kingFinanceReceiptBillEntityService.draft(kingFinanceReceiptBillEntity);
    }

    @Log(title = "财务模块-应收票据,推金蝶", businessType = BusinessType.INSERT)
    @PostMapping("submit")
    @ApiOperation("新增财务模块-应收票据,推金蝶")
    public AjaxResult submit(@RequestBody KingFinanceReceiptBillEntity kingFinanceReceiptBillEntity) {
        return kingFinanceReceiptBillEntityService.submit(kingFinanceReceiptBillEntity);
    }

    /**
     * 修改财务模块-应收票据,推金蝶
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:edit')")
    @Log(title = "财务模块-应收票据,推金蝶", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改财务模块-应收票据,推金蝶")
    public AjaxResult edit(@RequestBody KingFinanceReceiptBillEntity kingFinanceReceiptBillEntity) {
        return kingFinanceReceiptBillEntityService.updateKingFinanceReceiptBillEntity(kingFinanceReceiptBillEntity);
    }

    /**
     * 拆分前置条件：
     * 1、必须审批完成
     * 2、票面金额大于0.01，勾选等分化票据，且结算状态为空或者背书退回，数据状态已审核，未质押，未托管
     *
     * @param kingFinanceReceiptBillEntity
     * @return
     */
    @PostMapping("queryStatusBySplit")
    @ApiOperation("查询应收票据是否能够拆分")
    public AjaxResult queryStatusBySplit(@RequestBody KingFinanceReceiptBillStatusDto kingFinanceReceiptBillEntity) {
        KingFinanceReceiptBillEntity receiptBillEntity = kingFinanceReceiptBillEntityService.getById(kingFinanceReceiptBillEntity.getId());
        if (receiptBillEntity == null) {
            return AjaxResult.error("选中的单据不存在，请确认后重试！");
        }
        if (!ConstUtil.STATE_FINISH.equals(receiptBillEntity.getState())) {
            return AjaxResult.error("选中的票据还没有审核，不能进行操作！");
        }
        if (Double.parseDouble(StringUtils.isBlank(receiptBillEntity.getFaceAmount()) ? "0" : receiptBillEntity.getFaceAmount()) < 0.01) {
            return AjaxResult.error("票面金额大于0.01");
        }
        if (!receiptBillEntity.getIsSeparate()) {
            return AjaxResult.error("所选票据必须勾选等分化票据");
        }
        if (receiptBillEntity.getSettlementStatus() != 1 && receiptBillEntity.getSettlementStatus() != 4) {
            return AjaxResult.error("结算状态必须为空或者背书退回");
        }
        return AjaxResult.success(receiptBillEntity);
    }

    /**
     * 查询应收票据状态
     *
     * @param kingFinanceReceiptBillEntity
     * @return
     */
    @PostMapping("queryStatus")
    @ApiOperation("获取金蝶-财务-查询应付票据状态")
    public AjaxResult queryStatus(@RequestBody KingFinanceReceiptBillStatusDto kingFinanceReceiptBillEntity) {
        KingFinanceReceiptBillEntity receiptBillEntity = kingFinanceReceiptBillEntityService.getById(kingFinanceReceiptBillEntity.getId());
        if (receiptBillEntity == null) {
            return AjaxResult.error("选中的单据不存在，请确认后重试！");
        }
        if (!ConstUtil.STATE_FINISH.equals(receiptBillEntity.getState())) {
            return AjaxResult.error("选中的单据还没有审核，不能进行操作！");
        }
        if (receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_SPILT) {
            if (kingFinanceReceiptBillEntity.getType() == 2 || kingFinanceReceiptBillEntity.getType() == 3 || kingFinanceReceiptBillEntity.getType() == 4 || kingFinanceReceiptBillEntity.getType() == 5) {
                return AjaxResult.error("票据结算状态是拆分，不允许背书，贴现以及到期收款！");
            }
        }
        if (receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_RECITE && kingFinanceReceiptBillEntity.getType() == 3) {
            return AjaxResult.error("已经背书的票据，不能重复背书！");
        }
        if (kingFinanceReceiptBillEntity.getType() == 4 && receiptBillEntity.getSettlementStatus() != KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_RECITE) {
            //只有已经背书的票据才能退回，也就是没有背书的票据不能退回
            return AjaxResult.error("票据还没有背书或者已经被退回，不能进行退回！");
        }
        if (kingFinanceReceiptBillEntity.getType() == 5 && receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_DISCOUNT) {
            return AjaxResult.error("票据结算状态为贴现，不能重复贴现！");
        }
        if (kingFinanceReceiptBillEntity.getType() == 6 && receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_DUE_RECEIPT) {
            return AjaxResult.error("票据结算状态为到期收款，不能重复收款！");
        }
        if (kingFinanceReceiptBillEntity.getType() == 7 && receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_REFUND) {
            return AjaxResult.error("票据结算状态为退票，不能重复退票！");
        }
        if (receiptBillEntity.getSettlementStatus() == KingConstUtil.FINANCE_RECEIPT_BILL_SETTLEMENT_STATUS_PLEDGE) {
            if (kingFinanceReceiptBillEntity.getType() == 3 || kingFinanceReceiptBillEntity.getType() == 3) {
                return AjaxResult.error("质押或托管状态下的票据，不能进行背书、退票、托管等操作！");
            }
        }
        return AjaxResult.success(receiptBillEntity);
    }

    /**
     * 应收票据拆分
     *
     * @param kingFinanceReceiptBillDto
     * @return
     */
    //@PreAuthorize ( "@ss.hasPermi('heart:KingFinanceReceiptBill:split')" )
    @Log(title = "财务模块-应收票据,拆分", businessType = BusinessType.UPDATE)
    @PostMapping("/split")
    @ApiOperation("应收票据,拆分")
    public AjaxResult split(@RequestBody KingFinanceReceiptBillDto kingFinanceReceiptBillDto) {
        AjaxResult split = kingFinanceReceiptBillEntityService.split(kingFinanceReceiptBillDto);
        return split;
    }

    /**
     * 应收票据背书
     *
     * @param kingFinanceReceiptBillReciteDto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:recite')")
    @Log(title = "财务模块-应收票据,背书", businessType = BusinessType.UPDATE)
    @PostMapping("/recite")
    @ApiOperation("应收票据,背书")
    public AjaxResult recite(@RequestBody KingFinanceReceiptBillReciteDto kingFinanceReceiptBillReciteDto) {
        AjaxResult recite = kingFinanceReceiptBillEntityService.recite(kingFinanceReceiptBillReciteDto);
        return recite;
    }

    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:query')")
    @GetMapping("getReciteRefund")
    @ApiOperation("获取背书退回数据：应收票据基础数据+背书明细列表")
    public AjaxResult getReciteRefund(String id) {
        return AjaxResult.success(kingFinanceReceiptBillEntityService.getReciteRefund(id));
    }

    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:reciteRefund')")
    @Log(title = "财务模块-应收票据,背书退回", businessType = BusinessType.UPDATE)
    @PostMapping("/reciteRefund")
    @ApiOperation("应收票据,背书退回")
    public AjaxResult reciteRefund(@RequestBody KingFinanceReceiptBillReciteDto kingFinanceReceiptBillReciteDto) {
        AjaxResult recite = kingFinanceReceiptBillEntityService.reciteRefund(kingFinanceReceiptBillReciteDto);
        return recite;
    }

    /**
     * 应收票据贴现
     *
     * @param kingFinanceReceiptSettlementBillEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:discount')")
    @Log(title = "财务模块-应收票据,贴现", businessType = BusinessType.UPDATE)
    @PostMapping("/discount")
    @ApiOperation("应收票据,贴现")
    public AjaxResult discount(@RequestBody KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity) {
        AjaxResult discount = kingFinanceReceiptBillEntityService.discount(kingFinanceReceiptSettlementBillEntity);
        return discount;
    }

    /**
     * 应收票据质押
     *
     * @param kingFinanceReceiptSettlementBillEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:pledge')")
    @Log(title = "财务模块-应收票据,质押", businessType = BusinessType.UPDATE)
    @PostMapping("/pledge")
    @ApiOperation("应收票据,质押")
    public AjaxResult pledge(@RequestBody KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity) {
        AjaxResult discount = kingFinanceReceiptBillEntityService.pledge(kingFinanceReceiptSettlementBillEntity);
        return discount;
    }

    /**
     * 应收票据撤押
     *
     * @param kingFinanceReceiptSettlementBillEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:unPledge')")
    @Log(title = "财务模块-应收票据,撤押", businessType = BusinessType.UPDATE)
    @PostMapping("/unPledge")
    @ApiOperation("应收票据,撤押")
    public AjaxResult unPledge(@RequestBody KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity) {
        AjaxResult discount = kingFinanceReceiptBillEntityService.unPledge(kingFinanceReceiptSettlementBillEntity);
        return discount;
    }

    /**
     * 应收票据收款
     *
     * @param kingFinanceReceiptSettlementBillEntity
     * @return
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:receipt')")
    @Log(title = "财务模块-应收票据,收款", businessType = BusinessType.UPDATE)
    @PostMapping("/receipt")
    @ApiOperation("应收票据,收款")
    public AjaxResult receipt(@RequestBody KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity) {
        AjaxResult discount = kingFinanceReceiptBillEntityService.receipt(kingFinanceReceiptSettlementBillEntity);
        return discount;
    }

    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:refund')")
    @Log(title = "财务模块-应收票据,退票")
    @PostMapping("/refund")
    @ApiOperation("应收票据,退票")
    public AjaxResult refund(@RequestBody KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity) {
        AjaxResult discount = kingFinanceReceiptBillEntityService.refund(kingFinanceReceiptSettlementBillEntity);
        return discount;
    }

    /**
     * 删除财务模块-应收票据,推金蝶
     */
    @PreAuthorize("@ss.hasPermi('heart:KingFinanceReceiptBill:remove')")
    @Log(title = "财务模块-应收票据,推金蝶", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除财务模块-应收票据,推金蝶")
    public AjaxResult remove(@PathVariable String[] ids) {
        return kingFinanceReceiptBillEntityService.deleteKingFinanceReceiptBillEntityByIds(ids);
    }
}
